package com.allchips.pin.threads;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.allchips.pin.domain.PinInfoLc;
import com.allchips.pin.enums.PinInfoSyncEnum;
import com.allchips.pin.service.PinInfoLcService;
import com.allchips.pin.util.PinMatchUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gary
 * @date 2022年07月12日 11:52
 */
@Component
@Slf4j
public class EsSyncRunning {
    @Resource
    PinInfoLcService pinInfoLcService;

    public void run() {
        List<PinInfoLc> list = null;
        do {
            //查询待同步的数据
            LambdaQueryWrapper<PinInfoLc> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(PinInfoLc::getSync, PinInfoSyncEnum.STANDARD.getStatusCode(), PinInfoSyncEnum.SYMBOL_STANDARD.getStatusCode());
            queryWrapper.select(PinInfoLc::getId, PinInfoLc::getPartNumber, PinInfoLc::getCateName, PinInfoLc::getKbaseCatId, PinInfoLc::getPacking, PinInfoLc::getStandardBrandId
                    , PinInfoLc::getStandardBrandName, PinInfoLc::getStandardPinDt);
            queryWrapper.last("limit 100");
            list = pinInfoLcService.list(queryWrapper);

            if (CollUtil.isEmpty(list)) {
                break;
            }
            List<PinInfoLc> updateList = new ArrayList<>();
            for (final PinInfoLc k1 : list) {
                String pin_dt = k1.getStandardPinDt();
                if (StrUtil.isBlank(pin_dt)) {
                    continue;
                }
                JSONObject pinJSON = JSONUtil.parseObj(pin_dt);
                pinJSON.set("count", pinJSON.size());
                pinJSON.set("partNumber", ObjectUtil.defaultIfNull(k1.getPartNumber(), ""));
                pinJSON.set("cate", ObjectUtil.defaultIfNull(k1.getCateName(), ""));
                pinJSON.set("kBaseCatId", ObjectUtil.defaultIfNull(k1.getKbaseCatId(), 0));
                pinJSON.set("packing", ObjectUtil.defaultIfNull(k1.getPacking(), ""));
                pinJSON.set("standardBrandId", ObjectUtil.defaultIfNull(k1.getStandardBrandId(), 0));
                pinJSON.set("standardBrandName", ObjectUtil.defaultIfNull(k1.getStandardBrandName(), ""));
                final String s = PinMatchUtil.insertPin(k1.getId().toString(), pinJSON);
                PinInfoLc update = new PinInfoLc();
                update.setId(k1.getId());
                if (StrUtil.isNotBlank(s)) {
                    update.setSync(PinInfoSyncEnum.EXCEPTION_DATA.getStatusCode());
                    update.setExceptionMsg(s);
                } else {
                    update.setSync(PinInfoSyncEnum.ES_INSERT.getStatusCode());
                }
                updateList.add(update);
            }
            pinInfoLcService.updateBatchById(updateList);
        } while (CollUtil.isNotEmpty(list));
    }
}
